package com.mxprojects.constructors
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;

	public class ButtonHelper
	{
		public static function handleMouseActions(target:DisplayObject, isEnabled:Boolean, clickFunction:Function = null):void {
			if(isEnabled){
				if(!target.hasEventListener(MouseEvent.ROLL_OVER)){
					target.addEventListener(MouseEvent.ROLL_OVER, rollOver, false, 0, true);
					target.addEventListener(MouseEvent.ROLL_OUT, rollOut, false, 0, true);
					if(clickFunction != null){
						target.addEventListener(MouseEvent.CLICK, clickFunction, false, 0, true);
					}
				}
			}else{
				target.removeEventListener(MouseEvent.ROLL_OVER, rollOver);
				target.removeEventListener(MouseEvent.ROLL_OUT, rollOut);
				if(clickFunction != null){
					target.removeEventListener(MouseEvent.CLICK, clickFunction);
				}
			}
		}
		
		private static function rollOver(event:MouseEvent):void {
			event.currentTarget.gotoAndPlay("over");
		}
		
		private static function rollOut(event:MouseEvent):void {
			event.currentTarget.gotoAndPlay("out");
		}
		
	}
}